// 不同路径2

package Leetcode;

class solution_063 {
    public int uniquePathsWithObstacles(int[][] obstacleGrid) {
        int m = obstacleGrid.length;
        int n = obstacleGrid[0].length;
        // dp[i][j]表示到达(i,j)的路径个数
        int[][] dp = new int[m][n];
        // 初始化，找出左边界核上边界的障碍物
        for (int i = 0; i < m; i++) {
            if (obstacleGrid[i][0] != 1)
                dp[i][0] = 1;
            else {
                for (int j = i; j < m; j++)
                    dp[j][0] = 0;
                break;
            }
        }

        for (int j = 0; j < n; j++) {
            if (obstacleGrid[0][j] != 1)
                dp[0][j] = 1;
            else {
                for (int k = j; k < n; k++)
                    dp[0][k] = 0;
                break;
            }
        }

        for (int i = 1; i < m; i++)
            for (int j = 1; j < n; j++) {
                if (obstacleGrid[i][j] == 1)
                    dp[i][j] = 0;
                else
                    dp[i][j] = dp[i][j - 1] + dp[i - 1][j];
            }

        return dp[m - 1][n - 1];
    }
}

public class Solution063 {

}
